﻿using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility1;

namespace eDream.admin
{
    public partial class chi_tiet_donhang : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                grvDonhang_Load();
                grvDiachi_Load();
            }
        }

        private void grvDonhang_Load()
        {
           
                DataClasses1DataContext dx = new DataClasses1DataContext();
                try
                {
                    Xuly xl = new Xuly();
                    DataTable tb = new DataTable();
                    SqlParameter[] arrParam =
                    {
                        new SqlParameter("@ma_dh", System.Data.SqlDbType.NVarChar),
                    };
                    arrParam[0].Value = Request.QueryString["k"];


                    tb = xl.ExcuteStoreTable("sp_getAll_donhang", arrParam);
                    string trangthai = tb.Rows[0]["trangthai"].ToString();
                    hdfId.Value = tb.Rows[0]["IDd"].ToString();
                    if (trangthai == "Mới đặt")
                        btnChuyen.Visible = true;
                    else if (trangthai == "Đang chuyển")
                    {
                        btnChuyen.Visible = true;
                        btnChuyen.Text = "Hoàn tất";
                    }
                    else
                    {
                        btnChuyen.Visible = false;
                        btnHuy.Visible = false;
                    }
                    grvDonhang.DataSource = tb;
                    grvDonhang.DataBind();

                    GridViewDataColumn col1 = grvDonhang.Columns["Thanhtien"] as GridViewDataColumn;
                    ASPxLabel lbtota1 = (ASPxLabel)grvDonhang.FindFooterCellTemplateControl(col1, "lbTong");
                    lbtota1.Text = Utils.GetMoneyString(double.Parse(tb.Rows[0]["thanhtien"].ToString())) + "đ";
                }
                catch (Exception) { }
            
        }


        private void grvDiachi_Load()
        {
            
                try
                {
                    Xuly xl = new Xuly();
                    DataTable tb = new DataTable();
                    SqlParameter[] arrParam =
                    {
                        new SqlParameter("@mdh", System.Data.SqlDbType.NVarChar),
                    };
                    arrParam[0].Value = Request.QueryString["k"];
                    tb = xl.ExcuteStoreTable("sp_selectChitiet_Donhang", arrParam);
                    grvDiachi.DataSource = tb;
                    grvDiachi.DataBind();
                }
                catch (Exception) { }
                
            
        }

        protected void btnChuyen_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            Don_hang dh = new Don_hang();
            dh = dx.Don_hangs.Single(p => p.IDd == int.Parse(hdfId.Value));
            if (btnChuyen.Text == "Hoàn tất")
            {
                dh.trangthai = "Đã chuyển";
                dh.flag = 3;
            }
            else
            {
                dh.trangthai = "Đang chuyển";
                dh.flag = 2;
            }
            dx.SubmitChanges();

            grvDonhang_Load();
            
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            Don_hang dh = new Don_hang();
            dh = dx.Don_hangs.Single(p => p.IDd == int.Parse(hdfId.Value));
            dh.trangthai = "Hủy";
            dh.flag = 4;
            dx.SubmitChanges();

            grvDonhang_Load();
      
        }

        
    }
}